/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("cw_djlc_cwzb_view",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.Finance.IServices;
using VOL.Core.Filters;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using VOL.Core.Utilities;
using VOL.Core.DBManager;
using Castle.Components.DictionaryAdapter;

namespace VOL.Finance.Controllers
{
    public partial class cw_djlc_cwzb_viewController
    {
        private readonly Icw_djlc_cwzb_viewService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public cw_djlc_cwzb_viewController(
            Icw_djlc_cwzb_viewService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }
        [ApiActionPermission()]
        public override ActionResult Update([FromBody] SaveModel saveModel)
        {
            return base.Update(saveModel);
        }
        WebResponseContent webResponse = new WebResponseContent();

        [HttpPost, Route("update1")]
        [ApiActionPermission()]
        public ActionResult Update1(string development_expense, string vehiclelogisticsfee, string dcjb, string factoryprice, string marginalrate, string air_bottle_testing, string cw_djlc_djzb_id)
        {
            var dapper = DBServerProvider.SqlDapper;
            try
            {
                dapper.BeginTrans();
                string sql, sql2 = "";
                //这么写要求边际率 出厂价格不能为空了
                if (dcjb == null || "null".Equals(dcjb))
                {
                    sql = "update cw_djlc_djzb set [dcjb] = null where cw_djlc_djzb_id = @djzbId";
                    dapper.ExcuteNonQuery(sql, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql = "update cw_djlc_djzb set [dcjb] = @dcjb where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql, new { dcjb, cw_djlc_djzb_id });
                }
                if ("null".Equals(marginalrate) || "".Equals(marginalrate))
                {
                    sql = "update cw_djlc_djzb set [marginalrate] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql = "update cw_djlc_djzb set [marginalrate] = @marginalrate where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql, new { marginalrate, cw_djlc_djzb_id });
                }
                if ("null".Equals(factoryprice) || "".Equals(factoryprice))
                {
                    sql2 = "update cw_djlc_djzb set [factoryprice] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [factoryprice] = @factoryprice where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { factoryprice, cw_djlc_djzb_id });
                }
                
                if ("null".Equals(air_bottle_testing) || "".Equals(air_bottle_testing))
                {
                    sql2 = "update cw_djlc_djzb set [air_bottle_testing] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [air_bottle_testing] = @air_bottle_testing where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { air_bottle_testing, cw_djlc_djzb_id });
                }
                if ("null".Equals(vehiclelogisticsfee) || "".Equals(vehiclelogisticsfee))
                {
                    sql2 = "update cw_djlc_djzb set [vehicle_logistics_fee] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [vehicle_logistics_fee] = @vehiclelogisticsfee where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { vehiclelogisticsfee, cw_djlc_djzb_id });
                }
                
                if ("null".Equals(development_expense) || "".Equals(development_expense))
                {
                    sql2 = "update cw_djlc_djzb set [development_expense] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [development_expense] = @development_expense where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { development_expense, cw_djlc_djzb_id });
                }

                dapper.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                dapper.Rollback();
                return Json(webResponse.Error("保存失败！"));
            }
            return Json(webResponse.OK("保存成功！"));
        }

    }
}
